package com.muyu.system.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.poi.ExcelUtil;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.log.annotation.Log;
import com.muyu.common.log.enums.BusinessType;
import com.muyu.system.domain.DanmoShoppingUser;
import com.muyu.system.service.IDanmoShoppingUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * 商品关注Controller
 *
 * @author yangle
 * @date 2024-09-29
 */
@RestController
@RequestMapping("/user")
public class DanmoShoppingUserController extends BaseController {
    @Autowired
    private IDanmoShoppingUserService danmoShoppingUserService;

    /**
     * 查询商品关注列表
     */
//    @RequiresPermissions("system:user:list")
    @PostMapping("/list")
    public Result list(@RequestBody  DanmoShoppingUser danmoShoppingUser) {
        PageHelper.startPage(danmoShoppingUser.getPageNum(), danmoShoppingUser.getPageSize());
        List<DanmoShoppingUser> list = danmoShoppingUserService.selectDanmoShoppingUserList(danmoShoppingUser);
        PageInfo<DanmoShoppingUser> danmoShoppingUserPageInfo = new PageInfo<>(list);
        return Result.success(danmoShoppingUserPageInfo);
    }

    /**
     * 导出商品关注列表
     */
//    @RequiresPermissions("system:user:export")
    @Log(title = "商品关注", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DanmoShoppingUser danmoShoppingUser) {
        List<DanmoShoppingUser> list = danmoShoppingUserService.selectDanmoShoppingUserList(danmoShoppingUser);
        ExcelUtil<DanmoShoppingUser> util = new ExcelUtil<DanmoShoppingUser>(DanmoShoppingUser.class);
        util.exportExcel(response, list, "商品关注数据");
    }

    /**
     * 获取商品关注详细信息
     */
//    @RequiresPermissions("system:user:query")
    @GetMapping(value = "/{id}")
    public Result getInfo(@PathVariable("id") Long id) {
        return success(danmoShoppingUserService.selectDanmoShoppingUserById(id));
    }

    /**
     * 新增商品关注
     */
//    @RequiresPermissions("system:user:add")
    @Log(title = "商品关注", businessType = BusinessType.INSERT)
    @PostMapping
    public Result add(@RequestBody DanmoShoppingUser danmoShoppingUser) {
        return toAjax(danmoShoppingUserService.insertDanmoShoppingUser(danmoShoppingUser));
    }

    /**
     * 修改商品关注
     */
//    @RequiresPermissions("system:user:edit")
    @Log(title = "商品关注", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result edit(@RequestBody DanmoShoppingUser danmoShoppingUser) {
        return toAjax(danmoShoppingUserService.updateDanmoShoppingUser(danmoShoppingUser));
    }

    /**
     * 删除商品关注
     */
//    @RequiresPermissions("system:user:remove")
    @Log(title = "商品关注", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public Result remove(@PathVariable Long[] ids) {
        return toAjax(danmoShoppingUserService.deleteDanmoShoppingUserByIds(ids));
    }


    /**
     * 旗舰店关注
     */
    @PostMapping("/addUserFlagship")
    public Result addUserFlagship(@RequestBody DanmoShoppingUser danmoShoppingUser) {
//        LoginUser loginUser = SecurityUtils.getLoginUser();
//        danmoShoppingUser.setUserId(loginUser.getUserid());
//        if (danmoShoppingUser.getUserId() == null) {
//            return Result.warn("请登录");
//        }
     DanmoShoppingUser danmoShoppingUsers =  danmoShoppingUserService.selectUserFlagship(danmoShoppingUser);
        if (danmoShoppingUsers != null) {
            return Result.warn("已关注");
        }
        danmoShoppingUserService.addUserFlagship(danmoShoppingUser);
        return Result.success("关注成功");
    }
    /**
     * 取消关注
     */
    @PostMapping("/deleteUserFlagship")
    public Result deleteUserFlagship(@RequestBody DanmoShoppingUser danmoShoppingUser) {
//        LoginUser loginUser = SecurityUtils.getLoginUser();
//        danmoShoppingUser.setUserId(loginUser.getUserid());
//        if (danmoShoppingUser.getUserId() == null) {
//            return Result.warn("请登录");
//        }
        danmoShoppingUserService.deleteUserFlagship(danmoShoppingUser);
        return Result.success("取消关注");
    }
}
